<?php

namespace app\common\lib;

use think\facade\Db;

class MakeSiteUser
{
    public $web;

    public function run($webid,$username)
    {
        if(empty($webid) || empty($username)){
            return null;
        }
        $this->web = connect($webid);
        if(empty($this->web)){
            return null;
        }
        switch ($this->web['cms_type']) {
            case 'destoon':
                $insertId = $this->destoon($username);
            break;
            case 'eyoucms':
                # code...
            break;
            case 'dedecms':
                # code...
            break;
        }
        return $insertId;
    }

    private function destoon($username)
    {
        $site_user = Db::connect('dynamic')->name('member')->field('userid')->where('username',$username)->find();
        if(!empty($site_user)){
            return true;
        }
        $user = Db::name('admin')->where('username',$username)->find();
        if(empty($user)){
            return null;
        }
        
        $password = 'zhandian888';
        $data['inviter'] = $data['username'] = $username;
        $data['company'] = $data['truename'] = $data['passport'] = $user['nickname'];
        $data['passsalt'] = $this->randStr(8);
        $data['paysalt'] = $this->randStr(8);
        $data['password'] = $this->destoon_password($password, $data['passsalt']);
		$data['payword'] = $this->destoon_password($password, $data['paysalt']);
        $data['edittime'] = $data['regtime'] = time();
        $data['career'] = '招商加盟资深顾问';
        $data['wxqr'] = 'https://www.17shangji.com/file/upload/202009/12/161442731.png';
        $data['admin'] = 2;
        $data['groupid'] = 1;
        $data['role'] = '编辑';
        $data['regid'] = 6;
        $data['areaid'] = 1;
        $data['loginip'] = $data['regip'] = '127.0.0.1';
        $data['note'] = '';
        $insertId = Db::connect('dynamic')->name('member')->insertGetId($data);
        if(!empty($insertId)){
            Db::connect('dynamic')->name('member_misc')->insert([
                'userid' => $insertId,
                'username' => $username,
                'branch' => '',
                'reply' => '',
                'black' => ''
            ]);
            Db::connect('dynamic')->name('company')->insert([
                'userid' => $insertId,
                'username' => $username,
                'groupid' => 1,
                'company' => $username,
                'thumb' => 'https://www.17shangji.com/file/upload/202008/15/234201472.png',
                'linkurl' => 'https://www.17shangji.com/index.php?homepage='.$username,
                'areaid' => 1
            ]);
            Db::connect('dynamic')->name('company_data')->insert([
                'userid' => $insertId,
                'content' => ''
            ]);
            Db::connect('dynamic')->name('message')->insert([
                'title' => '站点系统',
                'typeid' => 4,
                'content' => '',
                'touser' => $username,
                'addtime' => time(),
                'ip' => '127.0.0.1',
                'status' => 3
            ]);
            return $insertId;
        }
    }

    private function randStr($length, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz') {
        $hash = '';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++)	{
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }
        
    private function destoon_password($password, $salt) {
        return md5(($this->destoon_is_md5($password) ? md5($password) : md5(md5($password))).$salt);
    }

    private function destoon_is_md5($password) {
        return preg_match("/^[a-f0-9]{32}$/", $password);
    }
    
}